package com.erp.system.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.*;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.system.domain.UserDO;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.system.domain.LeaveDO;
import com.erp.system.service.LeaveService;

import javax.servlet.http.HttpServletResponse;

/**
 * 请假流程
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-09-24 13:27:40
 */
 
@Controller
@RequestMapping("/system/leave")
public class LeaveController extends BaseController {
	@Autowired
	private LeaveService leaveService;

	@Autowired
	private UserService userService;
	@Autowired
	private NotifyService notifyService;
	
	@GetMapping()
	@RequiresPermissions("system:leave:leave")
	String Leave(){
	    return "system/leave/leave";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		//查询当前登录人的角色信息是否有部门经理和总经理
		int mgrcount = userService.isManagerOrCeo(getUserId());
		if(mgrcount <=0 ){
			params.put("userIdCreate",getUserId());
		}
		params.put("userIdCreate",getUserId());
        Query query = new Query(params);
		List<LeaveDO> leaveList = leaveService.list(query);
		int total = leaveService.count(query);
		PageUtils pageUtils = new PageUtils(leaveList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/leave/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		LeaveDO leave = leaveService.get(id);
		model.addAttribute("leave", leave);
	    return "system/leave/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( LeaveDO leave){
		leave.setUserIdCreate(getUserId());
		leave.setUserNameCreate(getUser().getName());
		leave.setGmtCreate(new Date());
		leave.setStatus(1);
		if(leave.getDeptManagerid() !=null){
			UserDO userDO = userService.get(leave.getDeptManagerid());
			leave.setDeptManagername(userDO.getName());
		}
		if(leaveService.save(leave)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( LeaveDO leave){
		leaveService.update(leave);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(leaveService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		leaveService.batchRemove(ids);
		return R.ok();
	}


	@PostMapping( "/okconfirm")
	@ResponseBody
	public R okconfirm(Long id, Integer status){
		LeaveDO leave = new LeaveDO();
		leave.setStatus(2);
		leave.setId(id);
		if(leaveService.update(leave)>0){
			LeaveDO leaveDO = leaveService.get(id);
			//发通知和邮件通知采购和总经理
			NotifyDO notify = new NotifyDO();
			notify.setType("6");
			notify.setTitle("请假申请");
			List<Long> deprids = new ArrayList<>();
			deprids.add(16L);
			List<UserDO> userDOList =userService.getUsersByDeptIds(deprids);
			List<Long> userId = userDOList.stream().map(UserDO::getUserId).collect(Collectors.toList());
			notify.setContent(leaveDO.getUserNameCreate()+"申请请假 /n"+
					"请假事由:"+leaveDO.getReason());
			userId.add(leaveDO.getDeptManagerid());
			Long[] userIds =userIds = userId.toArray( new Long[userId.size()]);
			notify.setUserIds(userIds);
			notify.setStatus("1");
			notify.setCreateBy(getUserId());
			notifyService.save(notify);
			return R.ok();
		}
		return R.error();
	}

	@GetMapping("/leavereport")
	String leavereport(){
		return "system/leave/leavereport";
	}


	@ResponseBody
	@GetMapping("/reportlist")
	public PageUtils reportlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<LeaveDO> leaveList = leaveService.reportlist(query);
		int total = leaveService.reportcount(query);
		PageUtils pageUtils = new PageUtils(leaveList, total);
		return pageUtils;
	}


	@PostMapping("/exportreportData")
	@ResponseBody
	public void downloadTemplate(LeaveDO leaveDO, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(leaveDO.getStatus() !=null){
			params.put("status",leaveDO.getStatus());
		}
		if(leaveDO.getStarttime() != null){
			params.put("starttime",leaveDO.getStarttime());
		}
		if(leaveDO.getEndtime() != null){
			params.put("endtime",leaveDO.getEndtime());
		}
		List<LeaveDO> leaveDOList = leaveService.reportlist(params);
		ExcelUtil.exportExcel(leaveDOList,"请假统计","请假统计",LeaveDO.class,"请假统计.xls",response);
	}

}
